<?php
ob_start(); 
set_time_limit(0);
ini_set('memory_limit','300M');
require('includes/top.php');

$target_path = "includes/Laptop_details/";
$target_pathFile = $target_path . basename($_FILES['upload_file']['name']); 


if(move_uploaded_file($_FILES['upload_file']['tmp_name'], $target_pathFile)) {
  $fcontents = file ('./'.$target_pathFile.''); 
 
 $fCount = 0;
  for($i=0; $i<sizeof($fcontents); $i++) { 
      $line = trim($fcontents[$i]); 
      $arr = explode("\t", $line); 
	  
	
	  if($i!=0)
	  {
	  
	  $sql = "REPLACE INTO temp_datacard_details(`datacard_name`,`card_no`,`RSN_no`,`box_no`,`status`,`depot_location`) values";
		$cCount = 0;
		$sizeOfCs = sizeof($arr);
		
    	$newarr = explode(',',$arr[0]);
			$cCount++;
			$datacard_name =  str_replace('"',"",$newarr['0']);
			$card_no =  str_replace('"',"",$newarr['1']);
			$RSN_no =  str_replace('"',"",$newarr['2']);
			$box_no =  str_replace('"',"",$newarr['3']);
			$status =  str_replace('"',"",$newarr['4']);
			$depot_location =  str_replace('"',"",$newarr['5']);
			
			if ($cCount > 1)
			{
				$sql .=",";
			}
			//build bulk insert
		   $sql .="('".$datacard_name."','".$card_no."','".$RSN_no."','".$box_no."','".$status."','".$depot_location."')";
		
		$result = mysql_query($sql);
		if ($result)
		{
			$fCount++;
			unlink($target_pathFile);
		}
	   $fCount." files processed into the temp_products table.\n";



}

	//execute bulk insert

}	  


//update existing products
$sql = "SELECT tp.* FROM temp_datacard_details tp LEFT JOIN data_card dc ON dc.datacard_id  = tp.datacard_id WHERE dc.datacard_id IS NOT NULL";

$result = mysql_query($sql);


if ($result)
{
	$countProds = 0;
	$sql = "REPLACE INTO data_card (`datacard_name`,`card_no`,`RSN_no`,`box_no`,`status`,`depot_location`) VALUES";
	while ($row = mysql_fetch_assoc($result))
	{
		
			$countProds++;
			if ($countProds > 1)
			{
				$sql .=",";
			}

			$sql .="('".$row['datacard_name']."','".$row['card_no']."','".$row['RSN_no']."','".$row['box_no']."','".$row['status']."','".$row['depot_location']."')";
	}
	   //   echo $sql;
	$result = mysql_query($sql);
	if ($result)
	{
		//echo $countProds." products updated in the system.\n";
	}
}

//now insert new products
$sql = "SELECT tp.* FROM temp_datacard_details tp LEFT JOIN data_card dc ON dc.datacard_id  = tp.datacard_id WHERE dc.datacard_id IS NULL";
$result = mysql_query($sql);

if ($result)
{
	$countProds = 0;
	$sql = "INSERT INTO data_card (`datacard_name`,`card_no`,`RSN_no`,`box_no`,`status`,`depot_location`) VALUES";
	while ($row = mysql_fetch_assoc($result))
	{
			$countProds++;
			if ($countProds > 1)
			{
				$sql .=",";
			}

			$sql .="('".$row['datacard_name']."','".$row['card_no']."','".$row['RSN_no']."','".$row['box_no']."','".$row['status']."','".$row['depot_location']."')";
	}
  	$result = mysql_query($sql);
	if ($result)
	{
		//echo $countProds." new products added to system.\n";
	}
}

$sql = "DELETE from temp_datacard_details";
$result = mysql_query($sql);
header('Location: add_datacard_details.php?v=success');
}
else
{
header('Location: add_datacard_details.php?v=err');
}
?>